package com.bw.user_module.user;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bw.common.entity.LiveDataEntity;
import com.bw.common.entity.RoomUserEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.common.provider.LoginProvider;
import com.bw.network_sdk.entity.BaseEntity;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class UserViewModel extends BaseViewModel<UserModel> {

    @Autowired
    LoginProvider loginProvider;

    @Override
    protected void initData() {
        ARouter.getInstance().inject(this);
        if (!loginProvider.isLogin()){
            ARouter.getInstance().build("/login/loginactivity").navigation();
            return;
        }
        userInfo();
    }

    public void userInfo(){
        loginProvider.getUserInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void onRestle(BaseEntity entity) {
        if (entity instanceof RoomUserEntity){
            RoomUserEntity roomUserEntity = (RoomUserEntity) entity;
            LiveDataEntity liveDataEntity = new LiveDataEntity();
            liveDataEntity.action = LiveDataEntity.LiveDataType.START_VIEW_MODEL;
            Map<LiveDataEntity.LiveDataType,Object> map = new HashMap<>();
            map.put(LiveDataEntity.LiveDataType.ENTITY,roomUserEntity);
            liveDataEntity.data = map;
            liveDataViewModel.post(liveDataEntity);
        }
    }
}
